﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BTask
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/6 15:05:46
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion

using K8.CBB;
using KingerRobot.MexicoSailunMDC.Spi;
using System.Linq;
using System.Security.Cryptography;

namespace KingerRobot.MexicoSailunMDC.SpiService;
internal class BBakTask : IBakTask
{
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static IBakTaskCmd _BakTaskCmd = new BBakTaskCmd();


    public List<SyncMesBean> GetWarehouseingBefore(SyncParm parm)
    {
        var db = Db.Default;
        var startTime = parm.StartTime;
        var endTime = parm.EndTime;
        var result = db.BakWcsTasks.Join(db.BakWcsTaskCmds, f => f.Id, s => s.TaskId, (f, s) => new
        {
            f.CreateTime,
            f.SkuId,
            s.TaskType,
            s.StartLoc,
        }).Where(t => t.CreateTime >= startTime
        && t.CreateTime < endTime
        && (t.StartLoc == 20010101 || t.StartLoc == 20010201)
        && t.TaskType == (int)EnumTaskType.Transportation).Select(t => new SyncMesBean()
        {
            LocId = t.StartLoc ?? 0,
            SkuId = t.SkuId ?? 0,
        }).ToList();
        return result;
    }

    public List<SyncMesBean> SyncWarehoused(SyncParm parm)
    {
        var db = Db.Default;
        var startTime = parm.StartTime;
        var endTime = parm.EndTime;
        var result = db.BakWcsTasks.Join(db.WcsLocSkus, f => f.SkuId, s => s.SkuId, (f, s) => f)
        .Where(t => t.CreateTime >= startTime
        && t.CreateTime < endTime
        && t.TaskType == (int)EnumTaskStatus.Finish
        && t.TaskType == (int)EnumTaskType.Warehousing).Select(t => new SyncMesBean()
        {
            LocId = t.TargetLoc ?? 0,
            SkuId = t.SkuId ?? 0,
        }).ToList();
        return result;
    }
    public List<POBakWcsTask> GetOutPutBefore(SyncParm parm)
    {
        var db = Db.Default;
        var startTime = parm.StartTime;
        var endTime = parm.EndTime;
        var result = db.BakWcsTasks.Join(db.BakWcsTaskCmds, f => f.Id, s => s.TaskId, (f, s) => new
        {
            f,
            s.TaskType,
            s.TargetLoc,
        }).Where(t => t.f.CreateTime >= startTime
        && t.f.CreateTime < endTime
        && t.TaskType == (int)EnumTaskType.Exwarehouse).Select(t => t.f).ToList();
        return result;
    }
    public List<POBakWcsTask> GetEMSOutPut(SyncParm parm)
    {
        var db = Db.Default;
        var startTime = parm.StartTime;
        var endTime = parm.EndTime;
        var result = db.BakWcsTasks.Join(db.BakWcsTaskCmds, f => f.Id, s => s.TaskId, (f, s) => new
        {
            f,
            s.TaskType,
            s.CmdStatus,
        }).Where(t => t.f.CreateTime >= startTime
        && t.f.CreateTime < endTime
        && t.CmdStatus == (int)EnumTaskStatus.Finish
        && t.TaskType == (int)EnumTaskType.EMS).Select(t => t.f).ToList();
        return result;

    }




    //--出库前
    //select* from bak_wcs_task_cmd bwtc where task_type = 2
    //and bwtc.create_time>='2023/10/12 04:00:00' and bwtc.create_time<'2023/10/13 13:00:00'

    //--出库后
    // select* from bak_wcs_task_cmd bwtc where  task_type=5 and cmd_status=9
    //and  bwtc.create_time>='2023/10/12 04:00:00' and  bwtc.create_time<'2023/10/13 13:00:00'







    public POBakWcsTask GetBakTaskByTaskNo(int taskNo)
    {
        var db = Db.Default;
        var wcsTask = db.BakWcsTasks.Where(t => t.TaskType == (int)EnumTaskType.EMS
        || t.TaskType == (int)EnumTaskType.Exwarehouse)
            .OrderByDescending(t => t.CreateTime).FirstOrDefault();

        // 超出时间范围无效
        if (wcsTask is not null)
        {
            if (wcsTask.CreateTime?.AddHours(4) < DateTime.Now)
            {
                return null;
            }
        }

        return wcsTask;
    }

    public POBakWcsTask GetBakTaskByProductCode(string productCode)
    {
        var skuId = _ProdProduct.GetSkuIdByCode(productCode);

        var db = Db.Default;
        var wcsTask = db.BakWcsTasks
            .Where(t => t.TaskType == (int)EnumTaskType.EMS
        || t.TaskType == (int)EnumTaskType.Exwarehouse
        && t.SkuId == skuId).OrderByDescending(t => t.CreateTime)
            .FirstOrDefault();

        // 超出时间范围无效
        if (wcsTask is not null)
        {
            if (wcsTask.CreateTime?.AddHours(4) < DateTime.Now)
            {
                return null;
            }
        }

        return wcsTask;
    }

    public POBakWcsTask GetBakTaskBySkuId(long skuId)
    {
        var db = Db.Default;
        var task = db.BakWcsTasks.Where(t => t.SkuId == skuId).FirstOrDefault();

        return task;
    }
    public POBakWcsTask GetBakTaskBySummaryId(long summaryId)
    {
        var db = Db.Default;
        var task = db.BakWcsTasks.Where(t => t.SummaryId == summaryId).FirstOrDefault();

        return task;
    }
    public int DeleteBakTaskByDateTime(DateTime dateTime)
    {
        var db = Db.Default;
        var bakTasks = db.BakWcsTasks
            .Where(t => t.BackTime < dateTime)
            .OrderBy(t => t.BackTime)
            .Take(10)
            .ToList();

        var taskIds = bakTasks.Select(t => t.Id).ToList();

        var result = DeleteBakTaskAndCmd(taskIds);

        return result;
    }

    private int DeleteBakTaskAndCmd(List<long?> taskIds)
    {
        var result = DeleteBakTask(taskIds);

        result += _BakTaskCmd.DeleteBakTaskCmd(taskIds);

        return result;
    }

    private int DeleteBakTask(List<long?> taskIds)
    {
        var db = Db.Default;
        var result = db.BakWcsTasks.Where(t => taskIds.Contains(t.Id)).Delete();
        return result;
    }
}